package com.gopay.common.cps.dao.order.impl;

import com.gopay.common.base.dao.impl.BaseReadDAOImpl;
import com.gopay.common.constants.SubAccountConstants;
import com.gopay.common.cps.dao.order.CpsGeneralReceiptOrderQueryDAO;
import com.gopay.common.domain.cps.CpsGenReceiptOrder;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收款类订单明细表DAO
 * 
 * CpsGeneralReceiptOrderQueryDAOImpl.java
 * @author MA HUAN
 * @2013-1-8 下午6:52:29 www.gopay.com.cn Inc.All rights reserved.
 */

@Repository(value="cpsGeneralReceiptOrderQueryDAO")
public class CpsGeneralReceiptOrderQueryDAOImpl extends BaseReadDAOImpl<CpsGenReceiptOrder, Serializable> implements
        CpsGeneralReceiptOrderQueryDAO {

    /** 获取累计成功退款金额及累计成功退还手续费金额HQL */
    private final String FIND_TOTAL_02_REFUND_AMT_HQL = "FROM CpsGenReceiptOrder r WHERE r.gopayOrderId=:gopayOrderId";
    
    @Override
    public BigDecimal findTotal02RefundAmt(String gopayOrderId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("gopayOrderId", gopayOrderId);

        List<CpsGenReceiptOrder> recList = this.findByHql(FIND_TOTAL_02_REFUND_AMT_HQL, param);
        if(recList != null && recList.size() > 0){
            return recList.get(0).getTotalRefundAmt().setScale(2);
        }
        return new BigDecimal("0").setScale(2);
    }

    @Override
    public BigDecimal findTotal02RefundFeeAmt(String gopayOrderId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("gopayOrderId", gopayOrderId);

        List<CpsGenReceiptOrder> recList = this.findByHql(FIND_TOTAL_02_REFUND_AMT_HQL, param);
        if(recList != null && recList.size() > 0){
            return recList.get(0).getTotalRefundFeeAmt().setScale(2);
        }
        return new BigDecimal("0").setScale(2);
    }

	@Override
	public BigDecimal findTotal02RefundUsedLimit(String gopayOrderId) {
		// TODO Auto-generated method stub
		Map<String, Object> param = new HashMap<String, Object>();
        param.put("gopayOrderId", gopayOrderId);

        List<CpsGenReceiptOrder> recList = this.findByHql(FIND_TOTAL_02_REFUND_AMT_HQL, param);
        if(recList != null && recList.size() > 0 && recList.get(0).getRefundUsedLimit() != null){
        	if(recList.get(0).getRefundUsedLimit().compareTo(new BigDecimal("0")) > 0){
        		return recList.get(0).getRefundUsedLimit().setScale(2);
        	}else{
        		return new BigDecimal("0").setScale(2);
        	}
            
        }
        return new BigDecimal("0").setScale(2);
	}

	@Override
    public Boolean isSpiltOrder(String gopayOrderId){
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(CpsGenReceiptOrder.class);
        if(StringUtils.isNotBlank(gopayOrderId)){
            detachedCriteria.add(Restrictions.eq("gopayOrderId", gopayOrderId));
        }
        List<CpsGenReceiptOrder> list = findByCriteria(detachedCriteria);
        if(list != null && list.size() > 0) {
            CpsGenReceiptOrder cpsGenReceiptOrder = list.get(0);
            if(StringUtils.isNotBlank(cpsGenReceiptOrder.getOtherRecvInfo())&& SubAccountConstants.SPLIT_STATUS_1.equals(cpsGenReceiptOrder.getOtherRecvStatus())){
                return true;
            }
        }
        return false;
    }

    @Override
    public CpsGenReceiptOrder findByGopayOrderId(String gopayOrderId){
        /*String hql = " from CpsGenReceiptOrder m where m.gopayOrderId = '"
                + gopayOrderId + "'";
        List<CpsGenReceiptOrder> ss = findByHql(hql);
        return (null == ss || 0 == ss.size()) ? null : ss.get(0);*/
        return find(CpsGenReceiptOrder.class,gopayOrderId);
    }
}
